package com.francisco.ets.util;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ca.etsmtl.log.util.IDLogger;

import com.francisco.ets.modele.Carre;
import com.francisco.ets.modele.Cercle;
import com.francisco.ets.modele.Forme;
import com.francisco.ets.modele.Ligne;
import com.francisco.ets.modele.Ovale;
import com.francisco.ets.modele.Rectancle;
public class GestionaireFormes 
{
	private static GestionaireFormes instance = null;
	private Forme[] listeFigures = new Forme[9];
	private int compteurFormes=0;
	private IDLogger logger;// Singleton

	//http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
	public static GestionaireFormes getInstance() {
      if(instance == null) {
         instance = new GestionaireFormes();
      }
      return instance;
   }
	public void ajouterFigure(String figString)
	{
		if(figString != null)
		{
			Forme nouvelleFigure;
			if(compteurFormes>8){compteurFormes=0;}
			Pattern patron = Pattern.compile(Labo1Const.PATRON_LIGNE_RECTANGLE_CARRE_OVALE);
			Matcher matcher = patron.matcher(figString);
			if(!matcher.matches())
			{
				patron = Pattern.compile(Labo1Const.PATRON_CERCLE);
				matcher = patron.matcher(figString);
			}
			if(matcher.matches())
			{
				
				switch(matcher.group(Labo1Const.FORME_TYPE))
				{   
					case Labo1Const.TYPE_FORME_LIGNE:
					{
						nouvelleFigure=new Ligne
								(Integer.parseInt(matcher.group(Labo1Const.FORME_NSEQ)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG1)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG2)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG3)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG4)));
								listeFigures[compteurFormes++]=nouvelleFigure;
								this.enregistrerFigure(nouvelleFigure.getNseq());
								break;
					}
					case Labo1Const.TYPE_FORME_CERCLE:
					{
						nouvelleFigure=new Cercle
								(Integer.parseInt(matcher.group(Labo1Const.FORME_NSEQ)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG1)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG2)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG3)));
							    listeFigures[compteurFormes++]=(nouvelleFigure);
							    this.enregistrerFigure(nouvelleFigure.getNseq());
							    break;
					}
					case Labo1Const.TYPE_FORME_OVALE:
					{
						nouvelleFigure=new Ovale
							(Integer.parseInt(matcher.group(Labo1Const.FORME_NSEQ)),
							Integer.parseInt(matcher.group(Labo1Const.FORME_ARG1)),
							Integer.parseInt(matcher.group(Labo1Const.FORME_ARG2)),
							Integer.parseInt(matcher.group(Labo1Const.FORME_ARG3)),
							Integer.parseInt(matcher.group(Labo1Const.FORME_ARG4))); 
							listeFigures[compteurFormes++]=nouvelleFigure;
							this.enregistrerFigure(nouvelleFigure.getNseq());
							break;
					}
					case Labo1Const.TYPE_FORME_CARRE:
					{
						nouvelleFigure=new Carre
								(Integer.parseInt(matcher.group(Labo1Const.FORME_NSEQ)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG1)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG2)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG3)));
								listeFigures[compteurFormes++]=nouvelleFigure;
								this.enregistrerFigure(nouvelleFigure.getNseq());
								break;
					}
					case Labo1Const.TYPE_FORME_RECTANGLE:
					{
						nouvelleFigure=new com.francisco.ets.modele.Rectancle
								(Integer.parseInt(matcher.group(Labo1Const.FORME_NSEQ)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG1)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG2)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG3)),
								Integer.parseInt(matcher.group(Labo1Const.FORME_ARG4)));
								listeFigures[compteurFormes++]=nouvelleFigure;
								this.enregistrerFigure(nouvelleFigure.getNseq());
								break;
					}
				}
			}	
		} 
		
	}
	public void dessinerFigures(Graphics g)
	{
			for(Forme fig :listeFigures)
			{
				if (fig!=null)
				{
					fig.drawFigure(g);
				}
			}
	}
	public Forme[]  getListeFigures() {
		return listeFigures;
	}
	public void enregistrerFigure(int nseq)
	{
		logger= IDLogger.getInstance();
		logger.logID(nseq);
	}
	
}
